package com.prince.web.controller;

import com.prince.aspect.AppLog;
import com.prince.model.Code;
import com.prince.page.Page;
import com.prince.util.MapUtil;
import com.prince.web.base.BaseController;
import com.prince.web.service.CodeService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class CodeController extends BaseController{

	@Resource
	private CodeService codeService;
	
	/**
	 * 分页查询
	 * @param params
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "selectByCode", method = RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> findCodeByPage(@RequestParam Map<String,Object> params) {
		Page page = getRequestPageRollInfo(params);
		Map<String,Object> map = codeService.findCodeByPage(params,page);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	/**
	 * 添加
	 * @param code
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "addCode", method = RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> addCode(HttpServletRequest request,Code code) {
		Map<String,Object> map = codeService.addCode(code);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	/**
	 * 修改
	 * @param code
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "updateCode", method = RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> updateCode(Code code) {
		Map<String,Object> map = codeService.updateCode(code);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	/**
	 * 删除
	 * @param code
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "delCode", method = RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> delCodeSort(Code code) {
		Map<String,Object> map = codeService.delCode(code);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	
	/**
	 * 根据条件查询code
	 * @param code
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "findCodeByCondition", method = RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> findCodeByCondition(Code code) {
		Map<String,Object> map = codeService.findCodeByCondition(code);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	/**
	 * 根据条件查询code
	 * @param code
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "findNameByCondition", method = RequestMethod.POST)
	public ResponseEntity<String> findNameByCondition(Code code) {
		String name ="查不到code值";
		if(null == code.getCode()|| code.getCode().equals("")){
			code.setCode(" ");
		}
		@SuppressWarnings("unchecked")
		List<Code> list = (List<Code>) codeService.findCodeByCondition(code).get("list");
		if (null!=list&&list.size()>0) {
			
			  name = list.get(0).getName();
		}
		return new ResponseEntity<String>(name,HttpStatus.OK);
	}
	/**
	 * 根据条件查询code
	 * @param code
	 * @return 返回list code
	 */
	@AppLog
	@RequestMapping(value = "findCodeListByCondition", method = RequestMethod.POST)
	public ResponseEntity<List<Code>> findCodeListByCondition(Code code) {
		@SuppressWarnings("unchecked")
		List<Code> list = (List<Code>) codeService.findCodeByCondition(code).get("list");
		return new ResponseEntity<List<Code>>(list,HttpStatus.OK);
	}
	
	
	
	/**
	 * 根据sortCode 查询  sort 字段 直接传排序
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "findCodebySortCode", method = RequestMethod.POST)
	public ResponseEntity<Map<String,Object>>  findCodeBySortCode(@RequestParam Map<String,Object> params) {
		Map<String,Object> map = new HashMap<>();
		if(MapUtil.isNotEmpty(params, "sort")) {
			map = codeService.findCodeBySortCode(params.get("sortCode").toString(),params.get("sort").toString());
		}else {
			map = codeService.findCodeBySortCode(params.get("sortCode").toString());
		}
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	/**
	 * 查询字典表所有sortcode 和code
	 * @return
	 */
	@AppLog
	@RequestMapping(value="findCodeListAndMap",method=RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> findCodeListAndMap(){
		Map<String,Object> map = codeService.findCodeListAndMap();
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	
	/**
	 * 根据sortCode 查询
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "findCodebySortCode1", method = RequestMethod.POST)
	@ResponseBody
	public List<Code>  findCodeBySortCode1(String sortCode) {
		List<Code> list = codeService.findCodeBySortCode1(sortCode);
		return list;
	}
	
	/**
	 * 查询多个字典
	 * @return
	 */
	@AppLog
	@RequestMapping(value="findDictCodeList",method=RequestMethod.POST)
	@ResponseBody
	public List<Code> findDictCodeList(@RequestParam Map<String,Object> param){
		List<Code> list = codeService.findDictCodeList(param);
		return list;
	}
}

